﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Common;
using Newtonsoft.Json;

namespace ConsoleApp
{
   public class ApiProxy
   {
       private static ApiProxy _apiProxy;

       private  static object _object=new object();

       /// <summary>
       /// Single
       /// </summary>
       /// <returns></returns>
       public static ApiProxy GetInstance()
       {
           if (_apiProxy == null)
           {
               lock (_object)
               {
                   if (_apiProxy == null)
                   {
                       _apiProxy =new ApiProxy();
                   }
               }
           }

           return _apiProxy;
       }


       /// <summary>
       /// 发送Get请求
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="request"></param>
       /// <returns></returns>
       public  Result<T> GetResult<T>(RequestContext<T> request)
       {
           var result = new Result<T>();
           HttpClient httpClient = new HttpClient();
           HttpResponseMessage response = httpClient.GetAsync(request.Uri).Result;
           result.DatList =
               JsonConvert.DeserializeObject<IEnumerable<T>>(response.Content.ReadAsStringAsync().Result);

           return result;
       }

       /// <summary>
       /// 发送Post请求
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="request"></param>
       /// <returns></returns>
       public Result<T> PostResult<T>(RequestContext<T> request)
       {
           var result = new Result<T>();
           HttpClient httpClient = new HttpClient();
           var requestJson = JsonConvert.SerializeObject(request.Data);
           HttpContent httpContent = new StringContent(requestJson);
           httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

           //发送请求
           HttpResponseMessage response = httpClient.PostAsync(request.Uri, httpContent).Result;

           if (response.IsSuccessStatusCode)
           {
               result.Code = "Success";
           }
           else
           {
               result.Message = "";
           }

          return result;
       }

       /// <summary>
       /// 
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="request"></param>
       /// <returns></returns>
       public Result<T> PutResult<T>(RequestContext<T> request)
       {
           var result = new Result<T>();

           HttpClient httpClient = new HttpClient();
           var requestJson = JsonConvert.SerializeObject(request.Data);
           HttpContent httpContent = new StringContent(requestJson);
           httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

           //发送请求
           HttpResponseMessage response = httpClient.PutAsync(request.Uri, httpContent).Result;

           if (response.IsSuccessStatusCode)
           {
               result.Code = "Success";
           }
           else
           {
               result.Message = "";
           }

           return result;
       }



       public Result<T>DeleteResult<T>(RequestContext<T> request)
       {
           var result = new Result<T>();
           HttpClient httpClient = new HttpClient();
           HttpResponseMessage response = httpClient.DeleteAsync(request.Uri).Result;
           if (response.IsSuccessStatusCode)
           {
               result.Code = "Success";
           }
           else
           {
               result.Message = "";
           }
           return result;
       }



   }
}
